package com.usian.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.usian.jwt.AppJwtUtil;
import com.usian.mapper.AdUserMapper;
import com.usian.model.admin.dtos.AdUserDto;
import com.usian.model.admin.pojos.AdUser;
import com.usian.model.base.BaseServiceImpl;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.service.AdUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import java.util.HashMap;
import java.util.Map;
@Service
public class AdUserServiceimpl extends BaseServiceImpl<AdUserMapper, AdUser> implements AdUserService {
    @Override
    public ResponseResult login(AdUserDto dto) {
        if (dto==null || StringUtils.isBlank(dto.getName()) || StringUtils.isBlank(dto.getPassword()))return paramError;
//         AdUser adUser = baseMapper.selectOne(Wrappers.<AdUser>lambdaQuery().eq(AdUser::getName, dto.getName()));
        AdUser one = getOne(Wrappers.<AdUser>lambdaQuery().eq(AdUser::getName, dto.getName()),true);
        if (one ==null) return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"用户不存在");
        String s = DigestUtils.md5DigestAsHex((dto.getPassword() + one.getSalt()).getBytes());
        if (s.equals(one.getPassword())){
            one.setPassword("");
            one.setSalt("");
            String token = AppJwtUtil.getToken(one.getId().longValue());
            Map<String, Object> objectMap = new HashMap<>();
            objectMap.put("user",one);
            objectMap.put("token",token);
            return ResponseResult.okResult(objectMap);
        }else{
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"密码错误");
        }
    }

//    public static void main(String[] args) {
//        String s = DigestUtils.md5DigestAsHex((1234 + "123456").getBytes());
//        System.out.println(s);
//        String s1 = DigestUtils.md5DigestAsHex((1212 + "9999").getBytes());
//        System.out.println(s1);
//    }


}
